export function addQueryParam(url: string, key: string, value: string | number | boolean) {
    if (url.indexOf('?') === -1) url += '?';
    else url += '&';
    url += encodeURIComponent(key) + '=' + encodeURIComponent(value);
    return url;
}

export function removeQueryParam(url: string, key: string) {
    if (url.indexOf('?') !== -1) {
        let arr = url.split('?')
        if (arr.length > 1) {
            let arr2 = arr[1].split('&')
            if (arr2.length) {
                for (let i in arr2) {
                    let arr3 = arr2[i].split('=')
                    if (arr3[0] == key) arr2.splice(parseInt(i), 1)
                }
                if (arr2.length) url = arr[0] + '?' + arr2.join('&')
                else url = arr[0]
            }
        }
    }
    return url;
}

export function goTo(url: string, is_switch = false) {
    if (url) {
        // #ifdef WEB
        if (url.indexOf("http") === 0) location.href = url
        // #endif
        // #ifdef APP
        if (url.indexOf("http") === 0) uni.navigateTo({ url: '/pages/common/web?url=' + url });
        // #endif
        if (is_switch) uni.reLaunch({ url: url });
        else uni.navigateTo({ url: url });
    }
}